#include "threadPool.h"
/* 发送响应（封装协议头） */
int sendCommand(int sockfd, CommandType_e cmd, ResponseResults_e res, const char *data, size_t data_len)
{
    protocol_header_t header = {
        .magic = MAGIC_NUMBER,
        .command = cmd,
        .data_length = data_len,
        .resp_type = res};

    // 非阻塞发送头
    size_t header_sent = 0;
    while (header_sent < HEADER_LENGTH) {
        int ret = send(sockfd, 
                      (char*)&header + header_sent, 
                      HEADER_LENGTH - header_sent, 
                      0);
        
        if (ret < 0) {
            // 非阻塞情况下正常重试
            if (errno == EAGAIN || errno == EWOULDBLOCK) {
                continue; // 等待下次可写事件
            }
            fprintf(stderr, "[ERROR] Header send failed\n");
            return -1;
        }
        header_sent += ret;
    }

    // 非阻塞发送负载
    if (data_len > 0 && data != NULL) {
        size_t data_sent = 0;
        while (data_sent < data_len) {
            int ret = send(sockfd, 
                          data + data_sent, 
                          data_len - data_sent, 
                          0);
            
            if (ret < 0) {
                // 非阻塞情况下正常重试
                if (errno == EAGAIN || errno == EWOULDBLOCK) {
                    continue; // 等待下次可写事件
                }
                fprintf(stderr, "[ERROR] Payload send failed\n");
                return -2;
            }
            data_sent += ret;
        }
    }
    
    return 0;
}

/* 接收请求（解析协议头） */
int recvCommand(int sockfd, protocol_header_t *header, char *buf, size_t buf_size)
{

    // 非阻塞接收头
    size_t header_received = 0;
    while (header_received < HEADER_LENGTH) {
        int ret = recv(sockfd, 
                      (char*)header + header_received, 
                      HEADER_LENGTH - header_received, 
                      0);
        
        if (ret < 0) {
            // 非阻塞情况下正常重试
            if (errno == EAGAIN || errno == EWOULDBLOCK) {
                continue; // 等待下次可读事件
            }
            fprintf(stderr, "[ERROR] Header receive failed\n");
            return -1;
        }
        
        // 连接关闭检测
        if (ret == 0) {
            fprintf(stderr, "[ERROR] Connection closed\n");
            return -5;
        }
        
        header_received += ret;
    }



    // 非阻塞接收负载
    if (header->data_length > 0) {
        // 缓冲区检查（保持原逻辑）
        if (header->data_length > buf_size) {
            fprintf(stderr, "[ERROR] Buffer too small (%zu < %d)\n",
                    buf_size, header->data_length);
            return -3;
        }
        printf("header->data_length = %d\n",header->data_length);
        size_t payload_received = 0;
        while (payload_received < header->data_length) {
            int ret = recv(sockfd, 
                          buf + payload_received, 
                          header->data_length - payload_received, 
                          0);
            
            printf("ret = %d\n",ret);
            if (ret < 0) {
                // 非阻塞情况下正常重试
                if (errno == EAGAIN || errno == EWOULDBLOCK) {
                    continue; // 等待下次可读事件
                }
                fprintf(stderr, "[ERROR] Payload receive failed\n");
                return -4;
            }
            
            // 连接关闭检测
            if (ret == 0) {
                fprintf(stderr, "[ERROR] Connection closed during payload\n");
                return -5;
            }
            
            payload_received += ret;
        }
    }
    
    return 0;
}